home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: nntp.coast.net!torn!nott!emr1!jagrant
- From: jagrant@emr1.emr.ca (John Grant)
- Subject: design problem
- Message-ID: <DMMwLp.Awv@emr1.emr.ca>
- Organization: Energy, Mines, and Resources, Ottawa
- References: <4flp0o$101@lal.interserv.net>
- Date: Sun, 11 Feb 1996 23:15:25 GMT
-
- I'm good at C, struggling at C++: I know the concepts (I can use existing
- classes), but I'll be damned if I can design/write my own.
-
- Problem
- -------
- I have several file types: A,B,C...
- Each one has an Open(), Close(), Draw() & Size() function, the nature
- of which is specific to the file type, but which is common to all file types.
- Each file type has specific functions as well, i.e. GetAStuff() for 'A' files.
-
- Implementation
- --------------
- I want to create a class I can use for each file, regardless of type. Later,
- I will want to put a bunch of these into a container, i.e. an array of some
- sort:
- cMYFILE *f1=new cMYFILE("xxx.a"); //file type 'A'
- cMYFILE *f2=new cMYFILE("yyy.b"); //file type 'B'
- ...
-
- I need to call the 'generic' functions which are common to all file types:
- f1->Open();
- f1->Draw();
- f1->Size();
-
- f2->Open();
- f2->Draw();
- f2->Size();
-
- However, I also need to be able to call functions which are specific to the
- file type. Presumably, I must first inspect the object to determine the
- file type:
- if(f1->Type()=='a') f1->GetAStuff();
- if(f1->Type()=='b') f1->GetBStuff();
-
- I haven't got a clue how to design these classes or how many I need. I started
- out with a base class:
- class cMYFILE{
- public: cMYFILE(char *name);
- ~cMYFILE();
- virtual int Open(void)=0;
- virtual int Draw(void)=0;
- virtual int Size(void)=0;
- virtual char Type(void)=0;
- };
-
- and then decided I needed to derive an A,B,C class from cMYFILE in order
- to implement the generic virtual functions and add in other file-specific
- functions:
- class cMYFILE_A:public cMYFILE{
- public: int Open(void);
- int Draw(void);
- int Size(void);
- int Type(void);
- int GetAStuff(void);
- };
-
- class cMYFILE_B:public cMYFILE{
- public: int Open(void);
- int Draw(void);
- int Size(void);
- int Type(void);
- int GetBStuff(void);
- };
-
- Then I got confused. I don't know which functions should be virtual,
- which functions should be pure virtual and what stuff should be in the
- base class and what stuff should be in the derived classes. I'm just
- confused.
-
- A related problem, which I haven't dealt with is to create a container
- class which will hold a list of these files, so I can do:
- nfile=filelist->nfile();
- for(i=0;i<nfile;i++){
- filelist->Open(i);
- filelist->Draw(i);
- filelist->Close(i);
- }
-
- Thanks for helping.
- --
- John A. Grant jagrant@emr1.emr.ca
- Airborne Geophysics
- Geological Survey of Canada, Ottawa
-